package com.liull.netty.nio.future;

import java.util.Random;
import java.util.concurrent.ExecutionException;

import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author: liull
 * @Description:
 * @Date: Created in 20:45 2021/7/24
 * @Modified By:
 */
@Slf4j
public class TestNettyFuture {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        NioEventLoopGroup group = new NioEventLoopGroup(3);
        final Future<Integer> future = group.next().submit(() -> {
            log.debug("执行计算");
            Thread.sleep(1000);
            return new Random().nextInt(10);
        });
        // 方法1
        log.debug("等待结果");
        // final Integer result = future.get();
        // log.debug("执行结束：{}", result);
        // 方法2
        future.addListener(new GenericFutureListener<Future<? super Integer>>() {
            @Override
            public void operationComplete(Future<? super Integer> future) throws Exception {
                log.debug("执行结束：{}", future.getNow());
            }
        });
    }
}
